package ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller;

import java.awt.Component;
import java.util.ArrayList;

import javax.swing.JLabel;

import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.database.DatabaseConnection;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.model.RFC;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.view.mainframe.generateRFCPanel.generateRFCSubs.DatainputPanel;

public class CMSThread extends Thread {

	protected void changeStateofComponents(ArrayList<Component> components) {

		for (Component component : components) {
			if (component.isEnabled()) {
				component.setEnabled(false);
			} else if (!component.isEnabled()) {
				component.setEnabled(true);
			}

		}
	}

	protected boolean saveRFCinDB(DatainputPanel datainputPanel, JLabel progress) {

		DatabaseConnection connection = new DatabaseConnection();
		connection.connect();

		try {

			String query = "UPDATE CMS_RFC SET " 
					+ RFC.TC_SUBMISSIONDATE + " = '"+ datainputPanel.getLb_dateValue().getText() + "',"
					+ RFC.TC_PRIORITY + " = '"+ datainputPanel.getCb_priorityValue().getSelectedItem().toString() + "',"
					+ RFC.TC_SUMMARY + " = '"+ datainputPanel.getTf_title().getText() + "',"
					//+ RFC.TC_DATEOFREJECTIONORAPPROVAL + " = '"+ datainputPanel.getTf_expirationdateValue().getText() + "',"
					+ RFC.TC_DETAILEDDESCRIPTION + " = '"+ datainputPanel.getTa_descriptionValue().getText() + "',"
					+ RFC.TC_CHANGEOWNER + " = '"+ datainputPanel.getCb_makerValue().getSelectedItem().toString() + "',"
					//+ RFC.TC_REASONFORCHANGE + " = '"+ datainputPanel.getTa_reasonValue().getText() + "',"
					//+ RFC.TC_COSTS + " = '"+ datainputPanel.getTa_costsValue().getText() + "',"
					//+ RFC.TC_BENEFITS + " = '"+ datainputPanel.getTa_benefitsValue().getText() + "',"
					//+ RFC.TC_RISKS + " = '"+ datainputPanel.getTa_risksValue().getText() + "',"
					+ RFC.TC_AFFECTEDSERVICES + " = '"+ datainputPanel.getAffectedServicesString() + "',"
					+ RFC.TC_AFFECTEDINFRASTRUCTURE + " = '"+ datainputPanel.getAffectedinfrastructuresString() + "'"
					+ " WHERE "
					+ RFC.TC_ID + " = '"
					+ datainputPanel.getLb_IDValue().getText() + "'";
			connection.insertquery(query);

			progress.setVisible(false);

		} catch (Exception e) {

			progress.setVisible(false);
			connection.disconnect();
			return false;
		}

		connection.disconnect();

		progress.setVisible(false);
		return true;
	}

}
